package org.jeecg.modules.wlhy.hyplatform.controller;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.paycenter.channel.bankdocking.dto.Acc;
import org.jeecg.modules.paycenter.channel.bankdocking.service.NhBankService;
import org.jeecg.modules.paycenter.channel.zxbank.entity.ZxAccountInfo;
import org.jeecg.modules.paycenter.channel.zxbank.utils.ZXUtil;
import org.jeecg.modules.paycenter.service.WalletService;
import org.jeecg.modules.wlhy.hyconfig.entity.HyConfig;
import org.jeecg.modules.wlhy.hyplatform.entity.HyPlatformAccountBook;
import org.jeecg.modules.wlhy.hyplatform.entity.HyPlatformBank;
import org.jeecg.modules.wlhy.hyplatform.service.IHyPlatformAccountBookService;
import org.jeecg.modules.wlhy.hyplatform.service.IHyPlatformBankService;
import org.jeecg.modules.wlhy.setting.service.IHySettingService;
import org.jeecg.modules.wlhy.utils.HyConfigUtils;
import org.jeecg.modules.wlhy.utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

 /**
 * @Description: hy_platform_account_book   农行垫付账户
 * @Author: jeecg-boot
 * @Date:   2021-08-28
 * @Version: V1.0
 */
@Api(tags="hy_platform_account_book")
@RestController
@RequestMapping("/hyplatform/hyPlatformAccountBook")
@Slf4j
public class HyPlatformAccountBookController extends JeecgController<HyPlatformAccountBook, IHyPlatformAccountBookService> {
	@Autowired
	private IHyPlatformAccountBookService hyPlatformAccountBookService;

	@Autowired
	private UserUtil userUtil;

	 @Autowired
	 private IHySettingService settingService;

	 @Autowired
	 private WalletService walletService;

	 @Autowired
	 private NhBankService nhBankService;

	 @Autowired
	 private IHyPlatformBankService hyPlatformBankService;
	 @Value("${pay-config.trade.begin:8}")
	 private String TRADE_BEGIN;

	 @Value("${pay-config.trade.end:19}")
	 private String TRADE_END;

	 @Value("${pay-config.cash-out.active:1}")
	 private String CASHOUT_ACTIVE;

	 @Value("${pay-config.cash-out.begin:8}")
	 private String CASHOUT_BEGIN;

	 @Value("${pay-config.cash-out.end:19}")
	 private String CASHOUT_END;

	 @Value("${pay-config.sms-active:0}")
	 private String CASH_OUT_SMS_ACTIVE;

	 @Value("${pay-config.acc-sms-active:0}")
	 private String ACC_SMS_ACTIVE;
	 @ApiOperation(value="获取平台多级账簿信息", notes="获取平台多级账簿信息")
	 @GetMapping(value = "/getAccountBookInfo")
	 public Result<?> getAccountBookInfo() {
		 if (DateUtil.hour(new Date(), true)>Integer.parseInt(TRADE_END) || DateUtil.hour(new Date(), true) < Integer.parseInt(TRADE_BEGIN)){
			 return Result.error("不在交易时间内, 详情请咨询系统管理员！");
		 }


		 if (!userUtil.isSys()) {
			 return Result.error("不是管理员,不能查看");
		 }
		 Map ret = new HashMap();
		 HyConfig config = HyConfigUtils.getConfig();
		 HyPlatformAccountBook accountBook = hyPlatformAccountBookService.getAccountBookInfo();
		 if (accountBook != null) {
			 String nhbankBankname = "";
			 if (config != null) {
				 nhbankBankname = config.getNhbankBankname();
			 }
			 accountBook.setKhh(nhbankBankname);
		 }
		 ret.put("nh", accountBook);
		 if (config.getZxIzUse()) {
			 ZxAccountInfo accountBalance = ZXUtil.getAccountBalance(config.getZxAccNo());
			 ret.put("zx", accountBalance);
		 }else {
			 ret.put("zx", null);
		 }
		 return Result.OK(ret);
	 }

	 @AutoLog(value = "开通多级账簿")
	 @ApiOperation(value="开通多级账簿", notes="开通多级账簿")
	 @PostMapping(value = "/addAccountBook")
	 @RequiresPermissions("platform:addAccountBook")
	 public Result<?> addAccountBook() {

		 if (DateUtil.hour(new Date(), true)>Integer.parseInt(TRADE_END) || DateUtil.hour(new Date(), true) < Integer.parseInt(TRADE_BEGIN)){
			 return Result.error("不在交易时间内, 详情请咨询系统管理员！");
		 }


		 if(!userUtil.isSys()){
			 return Result.error("不是管理员,不能开通");
		 }
		 hyPlatformAccountBookService.addAccountBook();
		 return Result.OK("添加成功！");
	 }

	 @AutoLog(value = "平台提现")
	 @ApiOperation(value="平台提现", notes="平台提现")
	 @PostMapping(value = "/tx")
	 public Result<?> tx(@RequestBody JSONObject jsonObject) {

		 if ("1".equalsIgnoreCase(CASHOUT_ACTIVE)
				 &&( DateUtil.hour(new Date(), true)>Integer.parseInt(CASHOUT_END) || DateUtil.hour(new Date(), true) <Integer.parseInt(CASHOUT_BEGIN))){
			 return Result.error("不在交易时间内, 详情请咨询系统管理员！");
		 }

		 HyConfig config = HyConfigUtils.getConfig();
		 if (!config.getNhbankIzUse()) {
			 return Result.error("未开通服务！");
		 }

		 String settingValue = settingService.getSettingValueByTag2("bank_relative");
		 if(!"1".equals(settingValue)) {
			 return Result.error("系统暂时关闭支付功能！");
		 }

		 String pwd = jsonObject.getString("pwd");
		 String samt = jsonObject.getString("amt");
		 String bankId = jsonObject.getString("bankId");

		 if(StringUtils.isEmpty(pwd)){
			 return Result.error("支付密码不能为空！");
		 }
		 if(StringUtils.isEmpty(bankId)){
			 return Result.error("银行卡不能为空！");
		 }
		 if(StringUtils.isEmpty(samt)){
			 return Result.error("提现金额不能为空！");
		 }
		 BigDecimal amt = null;
		 try{
			 amt = new BigDecimal(samt);
		 }catch (Exception e){
			 return Result.error("提现金额格式错误！");
		 }
		 // 0 > amt
		 if(BigDecimal.ZERO.compareTo(amt) == 1){
			 return Result.error("提现金额错误！");
		 }
		 if(!userUtil.isSys()){
			 return Result.error("无操作权限！");
		 }
		 if(!walletService.checkPwd(pwd)){
			 return Result.error("支付密码错误");
		 }

		 HyPlatformBank platformBank = hyPlatformBankService.getById(bankId);
		 if(platformBank == null){
			 return Result.error("银行卡信息不存在");
		 }

		 HyPlatformAccountBook accountBook = hyPlatformAccountBookService.getAccountBook();
		 if (accountBook == null) {
			 return Result.error("未开通垫付账户");
		 }

		 Acc acc = nhBankService.getAccBookInfo(accountBook.getAccNo(), accountBook.getLogAccNo(), accountBook.getProv());
		 if(acc == null){
			 return Result.error("查询余额失败");
		 }
		 //可用余额
		 BigDecimal availBal = acc.getAvailBal();
		 if (amt.compareTo(availBal) == 1) {
			 return Result.error("账户余额不足");
		 }
		 //提现
		 Result result = nhBankService.platformTx(accountBook, amt ,platformBank);
		 return result;
	 }
}
